package org.ssmd3.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.ssmd3.pojo.User;
import org.ssmd3.service.UserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")


public class AdminController {

    @Resource
    UserService userService;


    @RequestMapping("/login")
    public String login(){
        return "/slogin";
    }

    @RequestMapping("/index")
    public String index(){
        return "/index";
    }

    @RequestMapping(value = "/dologin")
    public String doLogin(Model model, HttpSession session, User user){
        User user1=userService.getUser(user.getUserName());
        if (user1!=null){
            if (user.getUserPassword().equals(user1.getUserPassword())){
                //在session里面添加用户状态信息
                session.setAttribute("uname",user1.getUserName());
                return "redirect:/admin/index";//重定向到这个页面
                //return "forwards:/home/index";转发到这个页面
            }else {
                model.addAttribute("error","密码错误");
            }
        }else {
            model.addAttribute("error","该用户名不存在");
        }
        return "/slogin";
    }
}

